package com.ybb.vue.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ybb.vue.common.Result;
import com.ybb.vue.domain.ApprovalStatusEnum;
import com.ybb.vue.domain.GoodsInStatusEnum;
import com.ybb.vue.domain.dto.ApprovalDetailDto;
import com.ybb.vue.domain.dto.ApprovalDto;
import com.ybb.vue.entity.Approval;
import com.ybb.vue.mapper.ApprovalMapper;
import com.ybb.vue.service.ApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author ybb
 * @version 1.0
 * @Description 审核controller
 * @Date 2023/4/4 22:00
 */
@RestController
@RequestMapping("/approval")
public class ApprovalController {

    @Autowired
    private ApprovalService approvalService;

    @Autowired
    private ApprovalMapper approvalMapper;

    @PostMapping("/inApproval")
    @PreAuthorize("hasAuthority('epidemic:goods:inapproval')")
    public Result<?> inApproval(@RequestBody @Valid ApprovalDto approvalDto) {
        return approvalService.approval(approvalDto);
    }

    @PostMapping("/outApproval")
    @PreAuthorize("hasAuthority('epidemic:goods:outapproval')")
    public Result<?> outApproval(@RequestBody @Valid ApprovalDto approvalDto) {
        return approvalService.approval(approvalDto);
    }

    @PostMapping("/approvalMsg")
    public Result<?> approvalById(@RequestBody @Valid ApprovalDetailDto approvalDetailDto) {
        Approval approval = approvalMapper.selectOne(new QueryWrapper<Approval>().lambda().eq(Approval::getSubjectId, approvalDetailDto.getSubjectId()).eq(Approval::getSubjectSource, approvalDetailDto.getSubjectSource()));
        approval.setApprovalStatus(ApprovalStatusEnum.getApprovalStatus(approval.getApprovalStatus()));
        return Result.success(approval);
    }

}
